// ignore_for_file: unnecessary_overrides, non_constant_identifier_names

import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:verse_systems/commom/routers/routes_pages_name.dart';
import 'package:verse_systems/net/http_dio.dart';
import 'package:verse_systems/pages/certification/certification_state.dart';
import 'package:verse_systems/utils/account_util.dart';
import 'package:verse_systems/utils/loading.dart';

class CertificationControl extends GetxController {


  final CertificationState state = CertificationState();

  void getAutoConfirm() async {
    final String token = await AccountUtil.getUserToken();
    final borrowId = Get.arguments["borrow_id"];
    Map<String, dynamic> map = HashMap();
    map['token'] = token;
    map['borrow_id'] = borrowId;
    Request.postCallback(
      HttpUrls.getAutoConfirm,
      data: map,
      success: (res) async {
        ResultData result = ResultData.fromJson(res.data);
        if (result.status == 0) {
          state.confirm_mode = result.data["confirm_mode"];
          // state.confirm_mode = 1;
          state.product = result.data["product"];
          state.confirm_quota_description =
              result.data["confirm_quota_description"];

          final Map amountInfo = state.product[0];
          debugPrint('amountInfo$amountInfo');

           //额度参数key
          state.amount = amountInfo["amount"]["key"].toString();
          state.amount_value = amountInfo["amount"]["value"].toString();

          final List dayInfo = amountInfo["day"];
          state.product_day = dayInfo;
          //期限参数key
          state.time = dayInfo[0]["key"].toString();
          state.time_value = dayInfo[0]["value"].toString();


          switch (state.confirm_mode) {
            case 0: //标准模式  正常展示产品供用户选择，用户选择后调用「额度确认页详细数据」展示详细信息，用户确认后调用「确认额度」
              getAutoConfirmDetailsInfo();
              break;

            case 1: //全自动模式 不展示页面，直接取产品信息中第一个金额以及金额里第一个期限调用「确认额度」（直接进入订单详情）
                onConfirmAmountToDetails();
              break;
            case 2: //半自动模式  展示期限/金额选择的页面，用户申请后取产品信息中第一个金额以及金额里第一个期限调用「确认额度」
                 update();
              break;

            case 3: //标准模式  正常展示产品供用户选择，用户选择后调用「额度确认页详细数据」展示详细信息，用户确认后调用「确认额度」
              getAutoConfirmDetailsInfo();
              break;

            default:
          }

        }
      },
    );
  }

  getAutoConfirmDetailsInfo() async {
    final String token = await AccountUtil.getUserToken();
    final borrowId = Get.arguments["borrow_id"];
    Map<String, dynamic> map = HashMap();
    map['token'] = token;
    map['borrow_id'] = borrowId;
    map['amount'] = state.amount;
    map['day'] = state.time;

    Request.postCallback(
      HttpUrls.getAutoConfirmDetails,
      data: map,
      success: (res) async {
        ResultData result = ResultData.fromJson(res.data);
        if (result.status == 0) {
          final List details = result.data["details"];
          state.confirmAmount = details;
          update();
        }
      },
    );
  }


  onConfirmAmountTomMode(){
      onConfirmAmountToDetails();
  }


  onConfirmAmountToDetails() async {
    final String token = await AccountUtil.getUserToken();
    final borrowId = Get.arguments["borrow_id"];
    Map<String, dynamic> map = HashMap();
    map['token'] = token;
    map['borrow_id'] = borrowId;
    map['amount'] = state.amount;
    map['day'] = state.time;
    map['ip'] = "192.168.2.1";

    debugPrint("确认额度-----$map");
    Request.postCallback(
      HttpUrls.getAutoConfirmProduct,
      data: map,
      success: (res) async {
        ResultData result = ResultData.fromJson(res.data);
        if (result.status == 0) {
          debugPrint("被送去风控，进入订单详情即可 ----->$result");
          Get.toNamed(RoutesPage.OrderDetails,
              arguments: {"borrow_id": borrowId, "product": true});
        } else {
          Loading.showError(result.msg);
        }
      },
    );
  }

 



  onIndexChangedAmount(index) {
    final Map amountInfo = state.product[index];
    debugPrint('amountInfo$amountInfo');
    state.amount = amountInfo["amount"]["key"].toString();
    state.amount_value = amountInfo["amount"]["value"].toString();
    final List dayInfo = amountInfo["day"];
    state.product_day = dayInfo;
    state.time = dayInfo[0]["key"].toString();
    state.time_value = dayInfo[0]["value"].toString();
    update();
    getAutoConfirmDetailsInfo();
  }

  onIndexChangeddayInfo(index) {
    final Map dayInfo = state.product_day[index];
    state.time = dayInfo["key"].toString();
    state.time_value = dayInfo["value"].toString();
    update();
    getAutoConfirmDetailsInfo();
  }

  @override
  void onInit() {
    super.onInit();
    getAutoConfirm();
  }
}
